GBML manual / Data Containers

Structs in data containers

A container holds one element type - a struct is how you keep several differently-typed, named values together in one slot, at no extra runtime cost.

One container, one type

Every container on this manual - array<T>, list<T>, grid<T>, stack<T>, queue<T>, map<K,V>, set<T> - holds exactly one element type, decided once. So can one container hold a string in one slot and a number in another? No - and that is not a missing feature, it is what makes every slot in a list of a thousand entries exactly the same size with no per-slot bookkeeping at all (below has the concrete cost of the alternative). A struct is the answer to "I need several different values that belong together in one slot": bundle them into one named value, then hold many of those in one ordinary, single-type container.

How: a struct literal as the element

/// @type {list<struct Loot>}
/// @capacity 4
var drops = [ { kind: 1, count: 3 } ];

drops[0].count = 5;
var now = drops[0].count; // 5

The /// @type tag names the container's element type as struct Loot - there is no separate place to declare what a Loot looks like first, the same as for any other struct (see Structs and constructors). The shape comes entirely from the literal itself: the first { kind: ..., count: ... } value GBML sees anywhere tagged struct Loot fixes those two fields, in that order, at those two types, for good. From then on, drops[0].kind/drops[0].count read and write straight through the index, chained - no separate var needed first - and assigning through the same chain (drops[0].count = 5) works exactly the same way.

Start it with one real value. A container whose declaration is only [] (or {}, for a map) gives GBML nothing to learn the shape from. The project still checks clean, but then the game fails to build, and the C++ compiler complains that the name "was not declared". For a list<struct Loot> or a stack<struct Loot>, write one value in the declaration and take it back out on the next line:

/// @type {list<struct Loot>}
var drops = [ { kind: 0, count: 0 } ];
list_clear(drops);                      // the shape is learned; the list starts empty

A map<K, struct X> has no way to do this, because a map cannot start with an entry in it. It can be shown a struct another way: map_create takes an example value, so map_create(8, "", { hp: 0, speed: 0 }) is an empty map whose values are structs - see Maps. The same trick works for a list, stack or queue (list_create(8, { kind: 0, count: 0 })), and then there is no struct to seed and clear.

Pros: several named fields, one slot, no extra cost

list<T>/array<T>/grid<T> all compile to one flat, fixed-size block of real values - every slot exactly sizeof(T), decided once, with no type tag anywhere and no branch on a read. A struct element changes nothing about that: list<struct Loot> is still one flat array of real Loot values, so bundling an int and an int into one named value costs nothing beyond the fields themselves.

Compare that to what a genuinely mixed-type slot would cost instead. GBML's untagged container capacity is 8 slots; an untagged list<real> is 8 x 4 bytes = 32 bytes total, since a real is a plain 4-byte number, nothing more. Give every one of those 8 slots room for a string instead, just in case one slot ever needed to hold text, and each slot alone costs roughly 48 bytes - a string with room for 32 characters (GBML's own default string capacity) carries a small fixed block of bookkeeping (a pointer and two whole numbers) on top of its own 32-character buffer - so the same 8 slots would cost nearly 400 bytes for a feature most of them would never actually use. A struct sidesteps the question entirely: every field's real type is fixed and known at compile time, so nothing ever needs a tag, a branch, or a worst-case size.

Cons: a fixed shape, and one real gap today

A struct's shape is fixed the moment its first literal appears - there is no adding a field to struct Loot later, once something else already relies on its shape (see Structs and constructors for why). Plan the fields a container's rows actually need up front, the same as you would for an object's own instance variables.

The one genuine, disclosed gap: reading a field off a struct that arrived as a script parameter does not check yet. Passing a list<struct Loot> argument into a script works fine - the parameter itself compiles, and so does the call - but the moment that script's own body reads a field off one of its elements, it is refused:

/// @arg {list<struct Loot>} items
function total_count(items) {
    return items[0].count;   // ERROR: this struct has no count field
}

This is not specific to list<struct T> - the identical message hits a bare struct-typed parameter too, since the tag alone has nowhere to read a real shape from once it is only a parameter. Script parameters covers this general rule in full. The workaround today: read the fields a script needs at the call site, where the real container's real shape is already known, and pass the plain values in instead of the struct itself:

function total_count(a_count, b_count) {
    return a_count + b_count;
}
// at the call site, where `items` is a real, resolved container:
var n = total_count(items[0].count, items[1].count);

A script that only ever passes its struct-shaped parameter along, without reading a field off it itself, is unaffected either way.

When a struct genuinely does not fit - only a couple of fields, or the point is walking one of them entirely on its own - a handful of parallel, same-length containers indexed together still works, at the cost of more code and one more way for two containers to drift out of sync; templates/sokoban's own undo history started out built that way, before list<struct T> compiled at all, and is one list<struct UndoMove> now (see Structs and constructors). Treat parallel containers as the fallback, not the default.

When to reach for one

Reach for a struct element when a container's rows are naturally a small bundle of differently-typed values that only make sense together - a saved undo step (a position, a moving reference, a couple more numbers), one row of a small lookup table, a spawn request (which enemy, where, facing which way). Reach for a plain single-type container - possibly several, side by side - when every slot really is just one number, string or reference on its own, or when the container's own element type needs to be searched, compared or hashed as a unit: map<K,V>'s value and set<T>'s element treat a struct differently for exactly that reason - see Data containers' own "Struct elements" section for the map/set-specific rule this page does not repeat.

Example

/// @type {list<struct Loot>}
/// @capacity 4
var drops = [ { kind: 1, count: 3 } ];

var before = drops[0].count; // 3
drops[0].count = 5;
var after = drops[0].count;  // 5

See also

Structs and constructors covers a struct's own shape rules in full, including the same script-parameter gap from the struct's own side. Script parameters covers everything else a container parameter does. Data containers covers the map/set hashability rule a struct element runs into on those two containers specifically.

See also: Data containersStructs and constructorsScript parameterslist_add